﻿using Entity;
using Struct;
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FastCopyWin
{
    internal class DeviceUtils
    {
        // 每个逻辑英寸沿屏幕宽度的像素数。 在具有多个显示监视器的系统中，此值对于所有监视器都是相同的。
        const int LOGPIXELSX = 88;

        const int SM_CXSCREEN = 0;
        const int SM_CYSCREEN = 1;

        /// <summary>
        /// 获取屏幕缩放比例系数
        /// </summary>
        /// <returns></returns>
        public static float GetPositionScale()
        {
            var hdc = Win32Api.GetDC(IntPtr.Zero);
            var dpiX = Win32Api.GetDeviceCaps(hdc, LOGPIXELSX);
            _ = Win32Api.ReleaseDC(IntPtr.Zero, hdc);
            var scale = Convert.ToInt32(dpiX / 96f * 100);
            return 100.0f / scale;
        }

        /// <summary>
        /// 获取屏幕逻辑宽高
        /// </summary>
        /// <param name="w">宽</param>
        /// <param name="h">高</param>
        public static void GetDeviceCaps(out float w, out float h)
        {
            var scale = GetPositionScale();
            w = Win32Api.GetSystemMetrics(SM_CXSCREEN) * scale; //屏幕宽度
            h = Win32Api.GetSystemMetrics(SM_CYSCREEN) * scale; //屏幕高度
        }

        public static void NotifySystemPaste(WindowData window)
        {
            Task.Run(() =>
            {
                Win32Api.SwitchToThisWindow(window.Hwnd, true);
                Win32Api.SetFocus(window.Hwnd);
                // 有些焦点恢复的慢 得稍微等一会再触发按键
                Thread.Sleep(60);
                INPUT[] inputs = new INPUT[4];
                inputs[0].type = 1;
                inputs[0].u = new();
                inputs[0].u.ki = new();
                inputs[0].u.ki.wVk = (ushort)Keys.RControlKey;

                inputs[1].type = 1;
                inputs[1].u = new();
                inputs[1].u.ki = new();
                inputs[1].u.ki.wVk = (ushort)Keys.V;

                inputs[2].type = 1;
                inputs[2].u = new();
                inputs[2].u.ki = new();
                inputs[2].u.ki.wVk = (ushort)Keys.V;
                inputs[2].u.ki.dwFlags = (uint)KEYEVENTF.KEYUP;

                inputs[3].type = 1;
                inputs[3].u = new();
                inputs[3].u.ki = new();
                inputs[3].u.ki.wVk = (ushort)Keys.RControlKey;
                inputs[3].u.ki.dwFlags = (uint)KEYEVENTF.KEYUP;
                // 避开左 Ctrl 一般没人会改右 Ctrl 的映射
                Win32Api.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
            });
        }

        /// <summary>
        /// 获取焦点窗口句柄
        /// </summary>
        /// <param name="hwnd">句柄</param>
        /// <param name="guiInfo">窗口信息</param>
        public static void GetCurrentForeWindowInfo(out WindowData window)
        {
            window = new(Win32Api.GetForegroundWindow());
            window.GuiInfo = Win32Api.GetGuiThreadInfo(window.Hwnd);
            window.ThreadId = Win32Api.GetWindowThreadProcessId(window.Hwnd, IntPtr.Zero);
        }
    }
}
